/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.server.tcp;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.tunnel.common.util.LogUtil;

import io.netty.channel.ChannelHandlerContext;

/**
 * 客户端通讯录
 */
public class TcpClientManager {
	/**
	 * 客户的通讯工具
	 * 客户端
	 */
	public static class TcpClient {

		/**
		 * 端口，唯一的
		 */
		private int port;
		
		/**
		 * 端口在tunnel_client那边的下标，通讯时候需要传递这个下标
		 */
		private int portIndex;
		
		/**
		 * 客户端名称
		 */
		private String clientName;
		
		private ChannelHandlerContext channelHandlerContext;
		
		public TcpClient(int port, String clientName, int portIndex) {
			this.port = port;
			this.clientName = clientName;
			this.portIndex = portIndex;
		}

		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}

		public int getPortIndex() {
			return portIndex;
		}

		public void setPortIndex(int portIndex) {
			this.portIndex = portIndex;
		}

		public String getClientName() {
			return clientName;
		}

		public void setClientName(String clientName) {
			this.clientName = clientName;
		}

		public ChannelHandlerContext getChannelHandlerContext() {
			return channelHandlerContext;
		}

		public void setChannelHandlerContext(ChannelHandlerContext channelHandlerContext) {
			this.channelHandlerContext = channelHandlerContext;
		}
		
	}

	private static Map<Integer,TcpClient> CLIENT_MAP = new HashMap<>();
	
	public synchronized static boolean add(TcpClient tunnel){
		if(!CLIENT_MAP.containsKey(tunnel.getPort())){
			LogUtil.log("online: tcp_"+tunnel.getPort()+"-"+tunnel.getClientName());
			
			CLIENT_MAP.put(tunnel.getPort(), tunnel);
			return true;
		}else{
			return false;
		}
	}
	
	public synchronized static TcpClient get(int port){
		return CLIENT_MAP.get(port);
	}
	
	public synchronized  static void remove(int port){
		TcpClient tunnel = CLIENT_MAP.remove(port);
		if(tunnel != null){
			LogUtil.log("offline: tcp_"+tunnel.getPort()+"-"+tunnel.getClientName());
		}
	}
	
	public synchronized  static Collection<TcpClient> getTunnels(){
		return CLIENT_MAP.values();
	}
}
